<?php
require 'core/header.php';

//--------------------


//获取用户信息
$user_all      = sql_search_uid_all( $_GET['uid'] );
$user_rankname = sql_search_rank_rankname( $user_all['user_rank'] );
$user_sexname  = sexnumberTOsexname( $user_all['user_sex'] );
//$user_birthday = birthdaynumberTObirthdaytext($user_all['user_birthday']);

$_SESSION['edituid'] = $_GET['uid'];


if ( isset( $_GET['mod'] ) && $_GET['mod'] == "notify" ) {
	notify();
}

?>


<hr>
<img src="<?= $user_all['user_himg'] ?>" width="96" height="96"/>
<?php
if ( isset( $_SESSION['uid'] ) ) {
	if ( $_SESSION['uid'] == $_GET['uid'] || $_SESSION['rank'] == 10 ) {
		echo '<form method="post" enctype="multipart/form-data" action="upload.php">更换头像：<input type="hidden" name="userid" val="' . $_GET['uid'] . '"/><input type="file" id="upImg" name="upImg"/><input type="submit" name="Submit" value="上传头像"/></form>';
	}
}
?>
<br/>
<?= $user_all['user_username'] ?> (UID:<?= $user_all['user_uid'] ?>)

<?php
if ( $user_all['user_logintime'] > time() ) {
	echo "<br /><span style='color:#FF0000'>封禁至：" . date( "Y-m-d H:i:s", $user_all['user_logintime'] ) . "</span>";
}
?>

<? //= $user_all['user_rank'] ?>
<br/>
用户组：<?= $user_rankname ?>
<!-- 编辑用户组 -->
<?php
if ( isset( $_SESSION['rank'] ) && $_SESSION['rank'] == 10 && $_GET['uid'] != $_SESSION['uid'] ) {
	echo '<select id="selectAll" onchange="getElementById(\'selectForum\').innerHTML=changeSelectRank(this.value)">';
	switch ( $user_all['user_rank'] ) {
		case "0":
			echo '
<option value="1" selected="selected">
        订阅者
    </option>
    <option value="2">
        板块编辑
    </option>
    <option value="3">
        管理员
    </option>';
			break;
		case "10":
			echo '<option value="1">
        订阅者
    </option>
    <option value="2">
        板块编辑
    </option>
    <option value="3" selected="selected">
        管理员
    </option>';
			break;
		default:
			echo '<option value="1">
        订阅者
    </option>
    <option value="2" selected="selected">
        板块编辑
    </option>
    <option value="3">
        管理员
    </option>';
			break;
	}
	echo '</select>';
	echo '<div id="selectForum">
</div>
<input type="button" onclick="ajaxpost_editrank(' . $_GET['uid'] . ')" value="修改"/>';
}
?>

<hr>
<?php
//获取徽章
$result = sql_search_medals( $_GET['uid'] );
while ( ( $result_array = mysqli_fetch_array( $result ) ) != null ) {
	$narray = sql_medalid_medals( $result_array['medal_id'] );
	if ( isset( $_SESSION['uid'] ) && $_SESSION['uid'] == $_GET['uid'] || isset( $_SESSION['rank'] ) && $_SESSION['rank'] == 10 ) {
		echo '<img src="' . $narray['medal_url'] . '" width="50px"
                height="50px" title="' . $narray['medal_name'] . '" onclick="var r = confirm(\'不会归还积分，确定删除?\'); if(r){ajaxpost_delUserMedal(' . $_GET['uid'] . ',' . $result_array['medal_id'] . ')}"/>';
	} else {
		echo '<img src="' . $narray['medal_url'] . '" width="50px"
                height="50px" title="' . $narray['medal_name'] . '"/>';
	}

}
if ( ! isset( $_GET['do'] ) || $_GET['do'] == 'profile' ) {
	$userwebsite_a = htmlspecialchars( $user_all['user_website'] );
	if ( $user_all['user_website'] != null ) {
		if ( strpos( "http", $userwebsite_a ) != 1 ) {
			$userwebsite_a = "http://" . $userwebsite_a;
		}
	}
	?>
    <h5>个人信息</h5>
    性别：<?= $user_sexname ?><br/>
    生日：<?= $user_all['user_birthday'] ?><br/>
    个人主页：<a href="<?= $userwebsite_a ?>"><?= htmlspecialchars( $user_all['user_website'] ) ?></a><br/>
    自我介绍：<?= htmlspecialchars( $user_all['user_ownspeak'] ) ?><br/>
    邮箱：
    <a href="mailto:<?= htmlspecialchars( $user_all['user_email'] ) ?>"><?= htmlspecialchars( $user_all['user_email'] ) ?></a></br/>
    <br/>
    加入时间：<?= date( "Y-m-d", $user_all['user_createtime'] ) ?><br/>
    最后一次登录时间：<?= date( "Y-m-d H:m:s", $user_all['user_lastlogin'] ) ?><br/>


	<?php
	if ( isset( $_SESSION['uid'] ) ) {

		if ( $_SESSION['rank'] == 10 || $_SESSION['uid'] == $_GET['uid'] ) {
			echo '<a href="home.php?mod=space&uid=' . $_GET['uid'] . '&do=change">修改资料</a>';
		}
	}
	?>
    <hr>
    <h5>统计信息</h5>
    主题：<a href="home.php?mod=space&uid=<?= $user_all['user_uid'] ?>&do=thread"><?= $user_all['user_posts'] ?></a><br/>
    等级：<?= $user_all['user_level'] ?><br/>
    经验：<?= $user_all['user_exp'] ?><br/>
	<?php
	if ( isset( $_SESSION['rank'] ) && $_SESSION['rank'] == 10 ) {
		echo sql_coin_name() . "：" . $user_all['user_coins'] . ' <br />增加修改：<input type="text" id="addCoins" placeholder="输入要增加的值"/><input type="button" onclick="ajaxpost_addCoin(' . $_GET['uid'] . ')" value="增加"><br />变更积分名称：<input type="text" id="scoreName" placeholder="如：金币"/><input type="button" onclick="ajaxpost_editCoinName(' . $_GET['uid'] . ')" value="修改"/> <br/>';
	} else {
		echo '积分：' . $user_all['user_coins'] . ' <br/>';
	}

} elseif ( $_GET['do'] == 'thread' ) {
	echo '<h5>主题</h5>';
	echo '<a href="home.php?mod=space&uid=' . $user_all['user_uid'] . '&do=thread&type=thread">主题</a>';
	echo '&nbsp;&nbsp;';
	echo '<a href="home.php?mod=space&uid=' . $user_all['user_uid'] . '&do=thread&type=reply">回复</a>';
	if ( isset( $_GET['type'] ) ) {
		if ( $_GET['type'] == "reply" ) {
			list_reply();
		} else {
			list_topics();
		}
	} else {
		list_topics();
	}

} elseif ( $_GET['do'] == 'change' ) {

	if ( isset( $_SESSION['uid'] ) ) {
		if ( ( $_SESSION['uid'] == $_GET['uid'] ) || $_SESSION['rank'] == 10 ) {
			if ( isset( $_GET['change'] ) ) {
				echo '<input type="hidden" id="uid" value="' . $_GET['uid'] . '"/>';
				if ( $_GET['change'] == 'username' ) {
					echo '<br /><input type="hidden" id="type" value="username"/><input type="text" id="change_username"/><input type="button" onclick="ajaxpost_change_users()" value="点击更改"/>';
				} elseif ( $_GET['change'] == 'password' ) {
					echo '<br /><input type="hidden" id="type" value="password"/>旧密码<input type="text" id="origin"/><br />新密码<input type="text" id="change_password"/><input type="button" onclick="ajaxpost_change_users()" value="点击更改"/>';
				}
			}
			if ( ! isset( $_GET['change'] ) || $_GET['change'] == 'password' ) {
				echo '&nbsp;<a href="home.php?mod=space&uid=' . $_GET['uid'] . '&do=change&change=username">修改用户名</a>';
			}
			if ( ! isset( $_GET['change'] ) || $_GET['change'] == 'username' ) {
				echo '&nbsp;<a href="home.php?mod=space&uid=' . $_GET['uid'] . '&do=change&change=password">修改密码</a>';
			}
			echo '<br/>';
			echo '性别：<select id="sex">';
			if ( $user_all['user_sex'] == 1 ) {
				echo '<option value="0">保密</option><option value="1" selected="selected">男</option><option value="2">女</option>';
			} elseif ( $user_all['user_sex'] == 2 ) {

				echo '<option value="0">保密</option><option value="1">男</option><option value="2" selected="selected">女</option>';
			} else {
				echo '<option value="0" selected="selected">保密</option><option value="1">男</option><option value="2">女</option>';
			}
			echo '</select><br/>
    生日：<input type="date" id="birthday_date" value="' . $user_all['user_birthday'] . '"/><br/>
    个人主页：<input type="text" id="website" value="' . htmlspecialchars_decode( $user_all['user_website'] ) . '"><br/>
    自我介绍：<input type="text" id="ownspeak" value="' . htmlspecialchars_decode( $user_all['user_ownspeak'] ) . '"/><br/>
    邮箱：<input type="text" id="email" value="' . htmlspecialchars_decode( $user_all['user_email'] ) . '"/><br/>
    签名：
    <!-- 加载编辑器的容器 -->
    <script id="container" name="content" type="text/plain">';
			echo htmlspecialchars_decode( $user_all['user_isignatur'] );
			echo '</script>
    <!-- 配置文件 -->
    <script type="text/javascript" src="utf8-php/ueditor.config.js"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="utf8-php/ueditor.all.js"></script>
    <!-- 实例化编辑器 -->
    <script type="text/javascript">
        var ue = UE.getEditor("container");
    </script>
<!-- 隐藏的post -->
    <input type="hidden" id="uid" value="' . $_GET['uid'] . '"/>
    <input type="button" onclick="ajaxpost_users()" value="保存"/>';
		} else {
			echo "您无权访问";
		}
	} else {
		echo "您无权访问";
	}
}
useCoin();
//积分兑换
function useCoin() {
	if ( isset( $_SESSION['uid'] ) && $_SESSION['uid'] == $_GET['uid'] || isset( $_SESSION['rank'] ) && $_SESSION['rank'] == 10 ) {
		?>
        <hr/>
        <h5>积分兑换</h5>

		<?php
		if ( isset( $_SESSION['rank'] ) && $_SESSION['rank'] == 10 && $_SESSION['uid'] == $_GET['uid'] ) {
			//echo '<a href="home.php?mod=space&uid='.$_GET['uid'].'&action=editMedal">添加徽章</a><br/>';
			?>
            <a href="#" onclick="getElementById('addMedal').innerHTML=addMedal(<?= $_GET['uid'] ?>)">添加徽章</a>
            <p id="addMedal"></p>
			<?php
		}
		$result = sql_list_medal();
		while ( ( $result_array = mysqli_fetch_array( $result ) ) != null ) {
			echo $result_array['medal_name'] . '（需要' . $result_array['medal_sale'] . sql_coin_name() . '）：<img src="' . $result_array['medal_url'] . '" width="50px"
                height="50px"/><input type="button" onclick="ajaxpost_buyMedal(' . $_GET['uid'] . ',' . $result_array['medal_id'] . ')" value="点击兑换"/><br/>';
		}
	}
}

//删除用户
if ( isset( $_SESSION['rank'] ) && $_SESSION['rank'] == 10 || isset( $_SESSION['uid'] ) && $_SESSION['uid'] == $_GET['uid'] ) {
	echo '<hr /><h5>用户操作</h5><input type="button" onclick="var r = confirm(\'确定删除?\');if (r) {ajaxpost_delUser(' . $_GET['uid'] . ');}" value="删除用户"/>';
}
?>

